package com.zq.widget.inverted.game.role.equipment.weapon.missile;

import android.content.Context;
import android.view.animation.AnimationUtils;

import com.zq.widget.inverted.game.role.impl.base.DrawableRole;
import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.hp.HPProxy;

/**
 * Created by zhangqiang on 2018/4/2.
 */
public class Missile extends DrawableRole implements Damage,HP{

    private float startX,startY;
    private int moveDegree;
    private float acceleration,startVelocity,maxVelocity;
    private long startTime;
    private Damage damageProxy;
    private HP hpProxy;

    public Missile(Context context, int drawableResId, float startX, float startY, int moveDegree, float acceleration, float startVelocity, float maxVelocity) {
        super(context, drawableResId);
        this.startX = startX;
        this.startY = startY;
        this.moveDegree = moveDegree;
        this.acceleration = acceleration;
        this.startVelocity = startVelocity;
        this.maxVelocity = maxVelocity;
        damageProxy = new DamageProxy(5);
        hpProxy = new HPProxy(10);
    }

//    @Override
//    protected Anim<?> onCreateAnim() {
//
//
//
//        return AnimProxy.ofPointF(new PointF(startX,startY),new PointF(endX,endY))
//                .setInterpolator(new AccelerateInterpolator())
//                .setTypeEvaluator(new BezierEvaluator(new PointF(startX + offsetLineLength,(endY + startY)/2)))
//                .setDuration(duration)
//                .setOnAnimUpdateListener(new OnAnimUpdateListener<PointF>() {
//                    @Override
//                    public void onAnimUpdate(Anim<PointF> anim) {
//                        updateLocation(anim.getAnimateValue());
//                    }
//                });
//    }


    @Override
    public boolean isFinish() {
        return super.isFinish() || isBeyondCanvas();
    }

    @Override
    public void preDraw() {
        super.preDraw();

        long timeMillis = AnimationUtils.currentAnimationTimeMillis();
        if(startTime <= 0){
            startTime = timeMillis;
        }
        float pastTime = (float) (timeMillis - startTime)/1000;

        float currentVelocity = Math.min(startVelocity + pastTime * acceleration,maxVelocity);

        float distance = currentVelocity * pastTime;

        int degree = moveDegree % 360;
        double radians = Math.toRadians(degree);
        float x = startX + (float) (Math.cos(radians) * distance);
        float y = startY -  (float) (Math.sin(radians) * distance);
        updateLocation(x,y);
    }

    @Override
    public long damage() {
        return damageProxy.damage();
    }

    @Override
    public long totalHp() {
        return hpProxy.totalHp();
    }

    @Override
    public long getHp() {
        return hpProxy.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpProxy.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpProxy.reduceHp(hp);
    }
}
